import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xuance/constant.dart';
import 'package:xuance/generated/assets.dart';
import 'package:xuance/util/ChartUtil.dart';
import 'package:xuance/widget/EmptyWidget.dart';
import 'package:xuance/widget/common_kv.dart';
import 'package:xuance/widget/expanded_widget.dart';

import 'logic.dart';

class WarningDetailPage extends StatelessWidget {
  const WarningDetailPage({super.key});

  @override
  Widget build(BuildContext context) {
    final WarningDetailLogic logic = Get.find<WarningDetailLogic>();
    final state = logic.state;
    return Scaffold(
        backgroundColor: bgColor,
        body: SafeArea(
            child:
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(children: [
                InkWell(
                    onTap: Get.back,
                    child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Icon(Icons.arrow_back_ios_new_outlined,
                            color: c_CCCCCF))),
                Text("数据详情", style: TextStyle(color: c_CCCCCF, fontSize: 17)),
              ])),
          Expanded(
              child: SingleChildScrollView(
            child: Column(
              children: [
                logic.bean.isWarnDetail
                    ? Column(
                        children: [
                          Obx(() {
                            return ExpandedWidget(
                                isExpand: state.warnMsgIsExpand.value,
                                onTap: () {
                                  state.warnMsgIsExpand.value =
                                      !state.warnMsgIsExpand.value;
                                },
                                assets: Assets.assetsWarnDetailMsg,
                                child: DefaultTextStyle(
                                    style: const TextStyle(
                                        fontSize: 15, color: Colors.white),
                                    child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Row(children: [
                                            const Text("告警时间："),
                                            Text(logic.bean.bean.warnTime ?? "")
                                          ]),
                                          const Row(children: [
                                            Text("告警来源："),
                                            Text("AI预警")
                                          ]),
                                          Row(children: [
                                            const Text("流量类型："),
                                            Text(
                                                logic.bean.bean.sceneType ?? "")
                                          ]),
                                        ])));
                          }),
                          const SizedBox(height: 10),
                        ],
                      )
                    : Container(),
                Obx(() {
                  return ExpandedWidget(
                      isExpand: state.currentSIPIsExpand.value,
                      onTap: () {
                        state.currentSIPIsExpand.value =
                            !state.currentSIPIsExpand.value;
                      },
                      assets: Assets.assetsTitleSip,
                      child: Column(children: [
                        const SizedBox(height: 10),
                        const Text("事件次数",
                            style:
                                TextStyle(color: Colors.white, fontSize: 14)),
                        const SizedBox(height: 10),
                        Obx(() {
                          return Text(
                              "${state.chartList.isEmpty ? 0 : state.chartList.map((f) => f.warnCount).reduce((a, b) => (int.tryParse('$a') ?? 0) + (int.tryParse('$b') ?? 0))}",
                              style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 24,
                                  fontWeight: FontWeight.bold));
                        }),
                        const SizedBox(height: 20),
                        AspectRatio(
                            aspectRatio: 2.1,
                            child: Obx(() {
                              if (state.chartList.isEmpty) {
                                return const EmptyWidget();
                              }
                              return LineChart(ChartUtil.getWarningChart(
                                  blockips: state.chartList.value,
                                  currentWarning: logic.type));
                            }))
                      ]));
                }),
                const SizedBox(height: 10),
                Obx(() {
                  return ExpandedWidget(
                      isExpand: state.sessionMsgIsExpand.value,
                      onTap: () {
                        state.sessionMsgIsExpand.value =
                            !state.sessionMsgIsExpand.value;
                      },
                      assets: Assets.assetsTitleSession,
                      child: DefaultTextStyle(
                          style: const TextStyle(
                              fontSize: 15, color: Colors.white),
                          child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Obx(() {
                                  return CommonKV(
                                      title: '从',
                                      value:
                                          '${state.sip.value}:${state.spt.value}');
                                }),
                                Obx(() {
                                  return CommonKV(
                                      title: '到',
                                      value:
                                          '${state.dip.value}:${state.dpt.value}');
                                }),
                                Obx(() {
                                  return CommonKV(
                                      title: '开始时间',
                                      value: state.startTime.value ?? "");
                                }),
                                Obx(() {
                                  return CommonKV(
                                      title: '结束时间',
                                      value: state.endTime.value ?? "");
                                }),
                                Obx(() {
                                  return CommonKV(
                                      title: "tcp flags",
                                      value: state.flags.value);
                                }),
                                Obx(() {
                                  return CommonKV(
                                      title: "协议",
                                      value: state.protocols.value);
                                }),
                                Obx(() {
                                  return CommonKV(
                                      title: "URI",
                                      value: state.uri.value ?? "");
                                }),
                                Obx(() {
                                  return CommonKV(
                                      title: "bytes", value: state.bytes.value);
                                }),
                              ])));
                }),
                const SizedBox(height: 10),
                Obx(() {
                  if (state.tcpFlow.isEmpty) {
                    return const EmptyWidget();
                  }
                  return ExpandedWidget(
                      isExpand: state.tcpTracebackIsExpand.value,
                      onTap: () {
                        state.tcpTracebackIsExpand.value =
                            !state.tcpTracebackIsExpand.value;
                      },
                      assets: Assets.assetsTcptracebackdoc,
                      child: DefaultTextStyle(
                          style: const TextStyle(
                              fontSize: 15, color: Colors.white),
                          child: Obx(() {
                            return Text(state.tcpFlow.value);
                          })));
                }),
              ],
            ),
          )),
        ])));
  }
}
